package com.java.zhuiyun.api.order.service.impl;

import com.java.zhuiyun.api.course_pc.service.CoursePcService;
import com.java.zhuiyun.api.order.entity.Course;
import com.java.zhuiyun.api.order.entity.FrontDeskUser;
import com.java.zhuiyun.api.order.entity.MemberType;
import com.java.zhuiyun.api.order.entity.Orders;
import com.java.zhuiyun.api.order.mapper.OrderMapper;
import com.java.zhuiyun.api.order.model.OrderModel;
import com.java.zhuiyun.api.order.service.OrderService;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName OrderServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/8/25 9:48
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CoursePcService coursePcService;

    /*
    * @Author wuwan
    * @Description 订单生成
    * @Date 2023/8/25 10:21
    * @Return
    * @param null
    */
    @Override
    public CommonResult orderAddition(OrderModel order) {
        String number = numberGenerate();
        //课程信息
        Course course = orderMapper.getCourse(order.getCid());
        FrontDeskUser frontDeskUser = orderMapper.userVip(order.getUid());
        Orders orders = new Orders();
//        //是否付款
//        String buyCourse = coursePcService.buyCourse(order.getCid(), order.getUid());
//        if (buyCourse.equals("已经购买此课程")){
//            return CommonResult.error("已经购买此课程");
//        }else if (buyCourse.equals("购买成功")){
//            orders.setOrderStatus(0);
//            orders.setOrderShipping("支付宝");
//        }else{
//            orders.setOrderStatus(1);
//        }
        // 课程价格
        BigDecimal coursePrice = course.getCoursePrice();
        orders.setOrderNumber(number);
        orders.setCreateTime(new Date());
        orders.setUpdateTime(new Date());
        //课程名称
        orders.setOrderName(course.getCourseTitle());
        //判断用户是否已经购买过此订单
        List<Orders> orders1 = orderMapper.selectOrderName(order.getUid());
        for (int i = 0; i < orders1.size(); i++) {
            Orders orders2 = orders1.get(i);
            if (orders2.getOrderName().equals(course.getCourseTitle())){
                return CommonResult.error("此订单已存在");
            }
        }
        //课程价格
        orders.setOrderPrice(coursePrice);
        //付款人手机号
        orders.setPaymentPhone(frontDeskUser.getFrontDeskPhone());
        // 数据状态
        orders.setDelFlag(0);
        // 付款人名称
        orders.setPaymentName(frontDeskUser.getFrontDeskName());
        // 付款人id
        orders.setUserId(order.getUid());
        // 课程id
        orders.setCourseId(order.getCid());
        //实际付款金额
        Integer frontDeskMember = frontDeskUser.getFrontDeskMember();
        MemberType memberType = orderMapper.getMemberType(frontDeskMember);
        Double discount = memberType.getDiscount();
        //优惠力度 类型转换
        BigDecimal discountBD = BigDecimal.valueOf(discount);
        // 折扣后价格计算
        BigDecimal paymentPrice = coursePrice.multiply(discountBD);
        if ( frontDeskMember == 4){
            orders.setPaymentPrice(coursePrice);
            orders.setOrderStatus(1);
            Integer integer = orderMapper.insert(orders);
            if (integer >= 1){
                return CommonResult.ok("下单成功请前去付款");
            }
            return CommonResult.error("下单失败");
        }
        orders.setPaymentPrice(paymentPrice);
        orders.setOrderStatus(1);
        Integer integer = orderMapper.insert(orders);
        if (integer >= 1){
            return CommonResult.ok("下单成功请前去付款");
        }
        return CommonResult.error("下单失败");
    }

    @Override
    public PageResult selectOrders(PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<Orders> orders = orderMapper.selectOrders();
        PageResult pageResult = MybatisPageHelper.getPageResult(orders);
        return pageResult;
    }

    /*
    * @Author wuwan
    * @Description 商品编号生成
    * @Date 2023/8/25 10:21
    * @Return
    * @param null
    */
    public String numberGenerate(){
        //获取当前时间 最后结果2023825
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        //设置时间格式
        String formattedDate = sdf.format(date);
        OrderModel qwe = orderMapper.numberGenerate();
        int count = qwe.getCount();
        int counts = count+1;
        String originalString = "00000";
        //获取编号  当前第几个订单 如第一个：000001
        DecimalFormat df = new DecimalFormat(originalString);
        String formattedString = df.format(counts);
        //编号拼接  2023825-000001
        StringBuilder stringBuilder = new StringBuilder(6);
        stringBuilder.append(formattedDate);
        stringBuilder.append("-");
        stringBuilder.append(formattedString);
        String s = stringBuilder.toString();
        return s;
    }
    /*
    * @Author wuwan
    * @Description 获取月卡会员折扣后课程价格
    * @Date 2023/8/30 11:31
    * @Return
    * @param null
    */
    @Override
    public CommonResult selectUserCourseData(Integer uid, Integer cid) {
        MemberType memberTypeMounth = orderMapper.getMemberTypeMounth();
        Double discount = memberTypeMounth.getDiscount();
        // 当前课程
        Course course = orderMapper.getCourse(cid);
        // 当前课程价格
        BigDecimal coursePrice1 = course.getCoursePrice();
        // 折扣后价格
        BigDecimal paymentPrice = coursePrice1.multiply(BigDecimal.valueOf(discount));
//        // 便宜后的价格  原价格 - 折扣价格 = 优惠价格
//        BigDecimal result = coursePrice1.subtract(paymentPrice);

        return CommonResult.ok(paymentPrice);
    }
    /*
    * @Author wuwan
    * @Description 实际付款价格
    * @Date 2023/8/30 11:23
    * @Return
    * @param null
    */
    @Override
    public CommonResult selectUserCourse(Integer uid, Integer cid) {
        // 用户数据 是什么会员
        FrontDeskUser frontDeskUser = orderMapper.userVip(uid);
        // 拿出用户的会员信息      也是会员的类型
        Integer frontDeskMember = frontDeskUser.getFrontDeskMember();
        // 查询出当前用户会员状态数据
        MemberType memberType = orderMapper.getMemberType(frontDeskMember);
        // 拿出当前用户会员状态的 折扣力度 百分比%
        Double discount = memberType.getDiscount();
        // 当前课程
        Course course = orderMapper.getCourse(cid);
        // 当前课程价格
        BigDecimal coursePrice1 = course.getCoursePrice();
        if (discount == 0.0){
            return CommonResult.ok(coursePrice1);
        }
        // 折扣后价格
        BigDecimal paymentPrice = coursePrice1.multiply(BigDecimal.valueOf(discount));

        return CommonResult.ok(paymentPrice);
    }

    @Override
    public Integer updateOrder() {
        Integer integer = orderMapper.updateOrder();
        return integer;
    }

    /*
     * @Author WX
     * @Description 查询单个记录
     * @Date 2023/9/2 10:45
     * @Return
     * @param null
     */
    @Override
    public Orders selectByTwoId(Integer uid, Integer cid) {
        return orderMapper.selectByTwoId(uid,cid);
    }

    @Override
    public CommonResult selorder(Integer cid, Integer uid) {
        return CommonResult.ok(orderMapper.selorder(cid,uid));
    }

}
